package midas.mw.crt.MD;

import java.util.Iterator;
import java.util.Vector;

import midas.mw.cfg.CfgLogging;
import midas.mw.cfg.log.Logger;
import midas.mw.crt.common.IPMessage;

public class MsgQueue
{
	/**
	 * List that stores messages into a LIFO structure (Last IN - First OUT).
	 */
	private Vector messagesQueue;

	/**
	 * Constructor - creates an instance of this class.
	 * This constructor creates the instance of MsgQueue and creates an empty
	 * ArrayList for the messagesQueue
	 */
	public MsgQueue ()
	{
		CfgLogging.logEvent(CfgLogging.LOG_MCALL, Logger.COMPONENT_CRT, CfgLogging.SEV_DEBUG, "MsgQueue()");
		
		messagesQueue = new Vector(); 
	}
	
	/**
	 * Returns the Top (Head) message at the messagesQueue (but the message is not
	 * erased).
	 * This method tries to retrieve the head of the LIFO messagesQueue. If the 
	 * messagesQueue is empty it's returns a null value.
	 * 
	 * @return IPMessage stored at the head of the messagesQueue, null if the
	 * 			 messagesQueue list is empty.
	 */
	public IPMessage seeTop()
	{
		//En este caso no hace falta sincronizar porque el unico sitio desde el que se la llama es desde
		//NodeListMD.seeMessageNode -> PriorityTable.seeFirstToBeSent y esta sincronizado		
		CfgLogging.logEvent(CfgLogging.LOG_MCALL, Logger.COMPONENT_CRT, CfgLogging.SEV_DEBUG, "MsgQueue.seeTop()");
		
		if (isEmpty())
			return null;
		else
			return (IPMessage)messagesQueue.get(0);
	}

	/**
	 * Inserts a IPMessage into the messagesQueue.
	 * This function inserts the IPMessage at the end (bottom) of the messagesQueue
	 * 
	 * @param ipMessage IPMessage to be insert in the messagesQueue structure.
	 */
	public void insert(IPMessage ipMessage) 
	{
		CfgLogging.logEvent(CfgLogging.LOG_MCALL, Logger.COMPONENT_CRT, CfgLogging.SEV_DEBUG, "MsgQueue.insert(IPMessage)");		
	
		messagesQueue.add(ipMessage);
	}

	/**
	 * Returns true if the messagesQueue is empty.
	 * This method delegates the operation to the ArrayList messagesQueue, 
	 * just calling ArrayList.isEmpty() function.
	 * 
	 * @return true if the messagesQueue has no messages stored and false otherwise
	 */
	public boolean isEmpty() 
	{
		CfgLogging.logEvent(CfgLogging.LOG_MCALL, Logger.COMPONENT_CRT, CfgLogging.SEV_DEBUG, "MsgQueue.isEmpty()");		

		return messagesQueue.isEmpty();
	}

	
	/**
	 * Stores in a string all the information from the messages stored in the
	 * messagesQueue. It is used to print ttl's out.
	 * 
	 * @return A characters's chain that represents all the information stored
	 * 			in the TimeQueue.
	 */
	public String passToString() 
	{
		//En este caso no hace falta sincronizar porque el unico sitio desde el que se la llama es desde
		//NodeListMD.printStruct -> PriorityTable.printTable y esta sincronizado
		CfgLogging.logEvent(CfgLogging.LOG_MCALL, Logger.COMPONENT_CRT, CfgLogging.SEV_DEBUG, "MsgQueue.passToString()");
		
		String sQueue;
		
		sQueue = new String("");
		Iterator itr = messagesQueue.iterator();
		
		while (itr.hasNext())
		{
			sQueue = sQueue.concat(((IPMessage)itr.next()).msgToString());
			sQueue = sQueue.concat("\r\n");
		}
			
		return sQueue;
	}

	/**	 
	 * Removes the IPMessage from the messagesQueue list.
	 * This method erases the IPMessage specified by the parameter from the 
	 * messagesQueue.
	 * 
	 * @param ipMessage IPMessage to be removed.
	 */
	public void deleteMessage(IPMessage ipMessage)
	{
		//En este caso no hace falta sincronizar porque el unico sitio desde el que se la llama es desde
		//NodeListMD.deletemessage -> PriorityTable.deleteMessage y esta sincronizado
		CfgLogging.logEvent(CfgLogging.LOG_MCALL, Logger.COMPONENT_CRT, CfgLogging.SEV_DEBUG, "MsgQueue..deleteMessage(IPMessage)");
		
		messagesQueue.remove(ipMessage);	
	}

}
